{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "import RPi.GPIO as GPIO\n",
    "import numpy as np\n",
    "import matplotlib as mpl\n",
    "import time\n",
    "\n",
    "def init(ch_list):\n",
    "    GPIO.setmode(GPIO.BCM)\n",
    "    GPIO.setup(ch_list, GPIO.OUT)\n",
    "    GPIO.output(ch_list, GPIO.LOW)\n",
    "\n",
    "def test_loop(ch_list, frequency):\n",
    "    for i in range(frequency):\n",
    "        for ch_i in ch_list:\n",
    "            GPIO.output(ch_i, GPIO.HIGH)\n",
    "            time.sleep(0.05)\n",
    "            GPIO.output(ch_i, GPIO.LOW)\n",
    "        \n",
    "#if __name__=='__main__':\n",
    "ch_list = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]\n",
    "frequency = 3\n",
    "init(ch_list)\n",
    "test_loop(ch_list, frequency)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "current ZT hr = 6.942222\n",
      "clk_hr = 15.000000 then zt_hr = 6.000000\n"
     ]
    }
   ],
   "source": [
    "def curr_zt(day_start_hr):\n",
    "    tms = time.localtime(time.time())\n",
    "    zt = tms.tm_hour + tms.tm_min/60.0 + tms.tm_sec/3600.0 - day_start_hr\n",
    "    if zt < 0:\n",
    "        zt += 24.0\n",
    "    return zt\n",
    "\n",
    "def get_zthr(clk_hr, day_start_hr):\n",
    "    zthr = clk_hr - day_start_hr\n",
    "    if zthr < 0 :\n",
    "        zthr += 24.0\n",
    "    return zthr\n",
    "\n",
    "day_start_hr = 9.0\n",
    "print('current ZT hr = %f' % curr_zt(day_start_hr))\n",
    "clk_hr = 15.0\n",
    "print('clk_hr = %f then zt_hr = %f' % (clk_hr, get_zthr(clk_hr, day_start_hr)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "current ZT-hr = 11.457222, stat_list: 1, 1, 1, 0, 0, 0, \n"
     ]
    }
   ],
   "source": [
    "def switch_lights(zthr, ch_list):\n",
    "    # 6 points over 24hr, 2 chanels for each point\n",
    "    # ch_list[0] & [1]: light on at ZT0 ~ ZT12\n",
    "    # ch_list[2] & [3]: light on at ZT4 ~ ZT16\n",
    "    # ch_list[4] & [5]: light on at ZT8 ~ ZT20\n",
    "    # ch_list[6] & [7]: light on at ZT12 ~ ZT0\n",
    "    # ch_list[8] & [9]: light on at ZT16 ~ ZT4\n",
    "    # ch_list[10] & [11]: light on at ZT20 ~ ZT8\n",
    "    stat_list = []\n",
    "    diff = 4\n",
    "    for i in range(6):\n",
    "        on_zthr = i*diff\n",
    "        off_zthr = (i*diff + 12) % 24\n",
    "        if on_zthr < off_zthr:\n",
    "            if zthr >= on_zthr and zthr < off_zthr:\n",
    "                GPIO.output(ch_list[i*2:(i+1)*2], GPIO.HIGH)\n",
    "                stat_list.append(1)\n",
    "            else:\n",
    "                GPIO.output(ch_list[i*2:(i+1)*2], GPIO.LOW)\n",
    "                stat_list.append(0)\n",
    "        else:\n",
    "            if (zthr >= on_zthr and zthr < 24) or zthr < off_zthr:\n",
    "                GPIO.output(ch_list[i*2:(i+1)*2], GPIO.HIGH)\n",
    "                stat_list.append(1)\n",
    "            else:\n",
    "                GPIO.output(ch_list[i*2:(i+1)*2], GPIO.LOW)\n",
    "                stat_list.append(0)\n",
    "    return stat_list\n",
    "            \n",
    "\n",
    "def test_schedule():\n",
    "    day_start_hr = 9.0\n",
    "    zthr = -1\n",
    "    for i in range(73):\n",
    "        clk_hr = i % 24\n",
    "        zthr = get_zthr(clk_hr, day_start_hr)\n",
    "        print('ZT-%d: ' % int(zthr), end='')\n",
    "        stat_list = switch_lights(zthr, ch_list)\n",
    "        [print('%d, ' % st, end='') for st in stat_list]\n",
    "        print()\n",
    "        time.sleep(0.5)\n",
    "    print()\n",
    "    \n",
    "            \n",
    "def start_schedule(day_start_hr=9.0):\n",
    "    zt = curr_zt(day_start_hr)\n",
    "    zthr = -1\n",
    "    while True:\n",
    "        zt = curr_zt(day_start_hr)\n",
    "        if zt - zthr > 1:\n",
    "            zthr = int(zt)\n",
    "            print('current ZT-hr = %f, stat_list: ' % zt, end='')\n",
    "            stat_list = switch_lights(zthr, ch_list)\n",
    "            #', '.join(stat_list)\n",
    "            [print('%d, ' % st, end='') for st in stat_list]\n",
    "            print()\n",
    "        time.sleep(1)\n",
    "#test_schedule()\n",
    "start_schedule()\n",
    "GPIO.cleanup()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
